﻿namespace LoveMusic
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Web.Routing;
    using System.Web.Mvc;
    using System.Web.Security;

    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
            CreateDefaultUserIfNotExists();
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            RegisterRoutesForNewIIS(routes);
        }

        private static void RegisterRoutesForNewIIS(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.Add(new Route("User/Login", new RouteValueDictionary(new { controller = "User", action = "Login" }), new MvcRouteHandler()));
            routes.Add(new Route("User/Logout", new RouteValueDictionary(new { controller = "User", action = "Logout" }), new MvcRouteHandler()));
            routes.Add(new Route("User/Signup", new RouteValueDictionary(new { controller = "User", action = "Signup" }), new MvcRouteHandler()));
            routes.Add(new Route("User/SendPassword", new RouteValueDictionary(new { controller = "User", action = "SendPassword" }), new MvcRouteHandler()));

            routes.Add(new Route("Artist/{artistName}", new RouteValueDictionary(new { controller = "Artist", action = "Index" }), new MvcRouteHandler()));
            routes.Add(new Route("Artist/SimilarArtist/{artistName}", new RouteValueDictionary(new { controller = "Artist", action = "SimilarArtist" }), new MvcRouteHandler()));
            routes.Add(new Route("Start", new RouteValueDictionary(new { controller = "Start", action = "Index" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/Detail/{id}", new RouteValueDictionary(new { controller = "Song", action = "Detail" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/GetStreamMp3/{mp3FileID}/{storyID}", new RouteValueDictionary(new { controller = "Song", action = "GetStreamMp3" }), new MvcRouteHandler()));
            routes.Add(new Route("Mp3/GetSongs/{storyId}/{artistId}", new RouteValueDictionary(new { controller = "Mp3", action = "GetSongs" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/Image/{id}/{width}/{height}", new RouteValueDictionary(new { controller = "Song", action = "Image" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/ImageStatic/{imageName}/{width}/{height}", new RouteValueDictionary(new { controller = "Song", action = "ImageStatic" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/Upcoming/{page}", new RouteValueDictionary(new { controller = "Song", action = "Upcoming" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/Tag/{name}/{page}", new RouteValueDictionary(new { controller = "Song", action = "Tag" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/PostedBy/{name}/{page}", new RouteValueDictionary(new { controller = "Song", action = "PostedBy" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/Search/{q}/{weight}/{searchType}", new RouteValueDictionary(new { controller = "Song", action = "Search" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/Search/{q}/{weight}/{searchType}/{songFormat}", new RouteValueDictionary(new { controller = "Song", action = "Search" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/Search/{q}/{weight}/{searchType}/{songFormat}/{page}", new RouteValueDictionary(new { controller = "Song", action = "Search" }), new MvcRouteHandler()));

            var defaults = new RouteValueDictionary(
                                                        new
                                                        {
                                                            controller = "Song",
                                                            action = "Category",
                                                            name = (string)null,
                                                            page = (int?)null
                                                        }
                                                    );

            var defaultsWeather = new RouteValueDictionary(
                                                       new
                                                       {
                                                           controller = "Song",
                                                           action = "CategoryWeather",
                                                           name = (string)null,
                                                           page = (int?)null
                                                       }
                                                   );

            routes.Add(new Route("Song/Category/{page}", defaults, new MvcRouteHandler()));

            routes.Add(new Route("Story/Category/{page}", defaults, new MvcRouteHandler()));
            routes.Add(new Route("Song/Category/{name}/{page}", defaults, new MvcRouteHandler()));
            //routes.Add(new Route("Song/{action}/{name}/{page}", defaults, new MvcRouteHandler()));

            routes.Add(new Route("Song/CategoryWeather/{page}", defaultsWeather, new MvcRouteHandler()));
            routes.Add(new Route("Song/CategoryWeather/{name}/{page}", new RouteValueDictionary(new { controller = "Song", action = "CategoryWeather" }), new MvcRouteHandler()));
            routes.Add(new Route("Song/TopPopular2010LoveSongs/{page}", new RouteValueDictionary(new { controller = "Song", action = "TopPopular2010LoveSongs" }), new MvcRouteHandler()));

            routes.Add(new Route("sitemap.xml", new RouteValueDictionary(new { controller = "Song", action = "Sitemap" }), new MvcRouteHandler()));
            routes.Add(new Route
            ("cache/{action}/{key}/{version}/{type}", new MvcRouteHandler())
                {
                    Defaults = new RouteValueDictionary(new { controller = "Cache", action = "CacheContent", key = "", version = "", type = "" }),
                }
            );

            routes.Add(new Route("thumbanial.aspx", new RouteValueDictionary(new { controller = "Song", action = "thumbanial" }), new MvcRouteHandler()));
            routes.Add(new Route("ThumbNew.aspx", new RouteValueDictionary(new { controller = "Song", action = "ThumbNew" }), new MvcRouteHandler()));
            routes.Add(new Route("FileStream.aspx", new RouteValueDictionary(new { controller = "Song", action = "FileStream" }), new MvcRouteHandler()));
            routes.Add(new Route("Top10.aspx", new RouteValueDictionary(
                new
                {
                    controller = "Song",
                    action = "Top10",
                    name = (string)null,
                    page = (int?)null
                }), new MvcRouteHandler()));

            routes.MapRoute(
                            "Default",                                              // Route name
                            "{controller}/{action}/{id}",                           // URL with parameters
                            new { controller = "Start", action = "Index", id = "1" }  // Parameter defaults
                            );



        }

        private static void CreateDefaultUserIfNotExists()
        {
            const string DefaultUser = "admin";
            const string DefaultPassword = "cat0dog";
            const string DefaultEmail = "admin@ILoveThisMusic.com";

            if (Membership.GetUser(DefaultUser) != null) return;

            Membership.CreateUser(DefaultUser, DefaultPassword, DefaultEmail);
        }
    }
}